package com.caifeng.myapplication.listview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import com.caifeng.myapplication.R;

import java.util.List;

public class MyAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<ListData> mListData;

    public MyAdapter(Context context, List<ListData> mListData) {
        this.inflater = LayoutInflater.from(context);
        this.mListData = mListData;
    }


    @Override
    public int getCount() {
        return mListData==null?0:mListData.size();
    }

    @Override
    public Object getItem(int position) {
        return mListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

     ViewHolder viewHolder;
    public class ViewHolder{
        ImageView imag;
        TextView text;

    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        viewHolder=null;
        if (view==null){
            viewHolder=new ViewHolder();
            view=inflater.inflate(R.layout.list_item,parent,false);

            viewHolder.imag=view.findViewById(R.id.imag);
            viewHolder.text=view.findViewById(R.id.name);
            view.setTag(viewHolder);

        }else {
           viewHolder=(ViewHolder)view.getTag();
        }

        viewHolder.imag.setImageResource(mListData.get(position).getImag());
        viewHolder.text.setText(mListData.get(position).getT());

        return view;
    }
}
